package com.caine.ts.ch06;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.caine_ts.R;

public class Lesson06 extends Activity {

	private int[] colors = new int[] { R.color.color1, R.color.color2,
			R.color.color3, R.color.color4, R.color.color5, R.color.color6,
			R.color.color7 };
	private int[] names = new int[] { R.id.tv01, R.id.tv02, R.id.tv03,
			R.id.tv04, R.id.tv05, R.id.tv06, R.id.tv07 };
	final TextView[] view = new TextView[7];
	int currentColor = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lesson06);

		for (int i = 0; i < names.length; i++) {
			view[i] = (TextView) findViewById(names[i]);
		}
		// =====================
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 0x1122) {
					for (int i = 0; i < names.length - currentColor; i++) {
						view[i].setBackgroundResource(colors[i+currentColor]);
					}
					
					for(int i = 7 - currentColor ,j = 0 ;i<7 ;i++,j++){
						view[i].setBackgroundResource(colors[j]);
					}
				}
				super.handleMessage(msg);
			}
		};
		// =====================
		new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				currentColor++;
				if(currentColor >= 6){
					currentColor = 0;
				}
				Message msg = new Message();
				msg.what = 0x1122;
				handler.sendMessage(msg);
			}
		},0,500);
	}

}
